﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WebForm_ManageCodeMapping_SearchCodeMapping : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (this.ddlTableName.SelectedValue == this.ddlTableName.PreviousSelectedValue)
        //{
        //    this.LoadTableNames();
        //}
        
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        this.ddlTableName.DataBound += new EventHandler(ddlTableName_DataBound);
        this.ddlColumnName.DataBound += new EventHandler(ddlColumnName_DataBound);
        this.gv.PageIndexChanging += new GridViewPageEventHandler(gv_PageIndexChanging);
    }

    void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gv.PageIndex = e.NewPageIndex;
        this.BindGV();
    }

    void ddlColumnName_DataBound(object sender, EventArgs e)
    {
        if (this.ddlColumnName.Items.Count > 1)
        {
            this.ddlColumnName.Items.Insert(0, MyObj.GetDDLPleaseChooseItem());
        }
        
        this.LoadTableNames();
    }

    protected void LoadTableNames()
    {
        if (this.ddlTableName.Items.Count == 0 || this.ddlColumnName.Items.Count == 0)
        {
            return;
        }
        
        CodeMappingDAO dao = new CodeMappingDAO();
        DataTable dt = dao.GetdllTableTextItem(this.ddlTableName.SelectedValue);

        foreach (ListItem li in this.ddlColumnName.Items)
        {
            if (li.Value == "")
            {
                li.Attributes.Add("TableName", "");
                continue;
            }

            dt.DefaultView.RowFilter = string.Format("ID = '{0}' ", li.Value);
            li.Attributes.Add("TableName", dt.DefaultView[0]["TableName"].ToString());
            li.Attributes.Add("ColumnName", dt.DefaultView[0]["ColumnName"].ToString());
        }
    }

    void ddlTableName_DataBound(object sender, EventArgs e)
    {
        this.ddlTableName.Items.Insert(0, MyObj.GetDDLPleaseChooseItem());
    }

    //查詢
    protected void btGetData_Click(object sender, EventArgs e)
    {
        BindGV();
    }

    //按下編輯
    protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }

    protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //GvCommand(this, new EventArgs());
    }

    //宣告編輯事件
    public delegate void UpdatingHandler(object sender, EventArgs e);
    public event UpdatingHandler Updating;

    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Session["TableName"] = ((Label)gv.Rows[e.NewEditIndex].FindControl("lblTableName")).Text;
        //Session["ColumnName"] = ((Label)gv.Rows[e.NewEditIndex].FindControl("lblColumnName")).Text;
        //Session["Value"] = ((Label)gv.Rows[e.NewEditIndex].FindControl("lblValue")).Text;

        this.TableName = ((Label)gv.Rows[e.NewEditIndex].FindControl("lblTableName")).Text;
        this.ColumnName = ((Label)gv.Rows[e.NewEditIndex].FindControl("lblColumnName")).Text;
        this.Value = ((Label)gv.Rows[e.NewEditIndex].FindControl("lblValue")).Text;

        e.Cancel = true;

        Updating(this, new EventArgs());
    }

    public void BindGV()
    {
        //MyObj.ShowMessage(this.ddlColumnName.SelectedItem.Value);
        this.LoadTableNames();

        if (this.ddlColumnName.Items.Count == 0)
        {
            return;
        }

        //MyObj.ShowMessage(this.ddlColumnName.SelectedItem.Attributes["TableName"] + " " + this.ddlColumnName.SelectedItem.Text);

        this.ObjectDataSource1.SelectParameters["TableName"].DefaultValue = this.ddlColumnName.SelectedItem.Attributes["TableName"];
        this.ObjectDataSource1.SelectParameters["ColumnName"].DefaultValue = this.ddlColumnName.SelectedItem.Attributes["ColumnName"];
        
        gv.DataSourceID = ObjectDataSource1.ID;
        gv.DataBind();
    }

    protected void ObjectDataSource1_Selecting(Object sender, ObjectDataSourceSelectingEventArgs e)
    {
        
    }

    public string TableName
    {
        get
        {
            return ViewState["TableName"].ToString();
        }
        set
        {
            ViewState["TableName"] = value;
        }
    }

    public string ColumnName
    {
        get
        {
            return ViewState["ColumnName"].ToString();
        }
        set
        {
            ViewState["ColumnName"] = value;
        }
    }

    public string Value
    {
        get
        {
            return ViewState["Value"].ToString();
        }
        set
        {
            ViewState["Value"] = value;
        }
    }
}

